package com.jlastu.znb.view;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;

public class AgricultureSkillDetailActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String URL_DETAILS = "file:///android_asset/page/details.html";
    private WebView mWebView;
    private Button mButton;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agriculture_skill_detail);
        mWebView = findViewById(R.id.webview);
        mButton = findViewById(R.id.button);
        mButton.setOnClickListener(this);
        WebSettings settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new WebAppInterface(), "myInterface");
        mWebView.loadUrl(URL_DETAILS);

    }

    @Override
    public void onClick(View v) {
        String title = "这个是新的标题"; //数据，可以是从数据库中获取的数据。
        mWebView.loadUrl("javascript:setTitle('" + title + "')");//调用html文件中的javascript方法，传入数据
    }

    public class WebAppInterface {
        @JavascriptInterface
        public void onTitleUpdate(boolean showToast, String newTitle) {
            // 此方法在html页面中被调用
            if (showToast) {
                Toast.makeText(AgricultureSkillDetailActivity.this, "新标题:" + newTitle, Toast.LENGTH_SHORT).show();
            }
        }

        @JavascriptInterface
        public void goBack(boolean showToast, String currentTitle) {
            // 此方法在html页面中被调用
            if (showToast) {
                Toast.makeText(AgricultureSkillDetailActivity.this, "当前标题:" + currentTitle, Toast.LENGTH_SHORT).show();
            }
            finish();
        }
    }
}
